<?php


function get($request){

	return $request->query();

}
function post($request){

	return $request->post();

}
function getUserSession(){


	return Session::instance('database')->get('user');
}
function checkUserSessionOrJumpLogin(){
	$result = Session::instance('database')->get('user');
	if($result == null){
		echo "<script>window.location.href='".Url::base()."index.php/login/login'</script>";
		die;
	}


	return  $result;
}
function checkUserSession(){

	if(getUserSession('user') == null){
		echo "<script>window.location.href='".Url::base()."index.php/login/login'</script>";
		die;
	}

}
function setUserSession($key,$value){
	Session::instance('database')->set($key,$value);
}
function success($result){
	$msg['status'] =   true;
	$msg['result']=$result;
	$msg['code']="10000";
	return $msg;
}
function error($e){
	$msg['status'] =   false;
	$msg['result']=$e;
	$msg['code']="20000";
	return $msg;
}
function redirect404($e=false){

	if(!$e){

		echo "<script>window.location.href='../../404.html'</script>";
	}else{
		echo "<script>window.location.href='../../404.html?".$e."'</script>";
	}
	
	exit;
}


function countOrder($orderList){
	$expressPrice =array();
	$default = 10;
	$postSum = true;
	if(count($orderList)>1){
		$postSum = false;

	}
	$count = 0;
	$str = null;

	foreach($orderList as $key=>$value){
		$parm[$key] =$value['title'];
		$postfee = 0 ;

		$parm['productId'] = $value['productId'];
		$config = rpcCall('activeconfig.getByProductIdAndActiveName',$parm);
		$falg = false;;
		if(count($config) !=0){
			if(intval($value['number'])>=$config[0]['parm']){
				$str=$str."&#60;".$parm[$key] .'>'.'商品满足'.$config[0]['name'].'活动<br>';
				$postfee = 0;
				$falg = true;
			}
		}
		if(!$falg ){
			$template =rpcCall('chinatemplate.getchinatemplate',$parm);

			if(count($template)!= 0 ){

				$morethan = $template[0]['morethan'];
				array_push($expressPrice,10);
				if($value['weight']>$morethan){
					$str=$str."&#60;".$parm[$key] .'>重量为'.$value['weight'].'商品超过'.$morethan.'公斤,每公斤运费为'.$template[0]['price'].'元<br>';
					$postfee = $postfee+($value['weight']-$morethan)/$template[0]['price'];
					//1+122
				}
				if($postSum ){
					$str=$str."&#60;".$parm[$key] .'>'.'加上普通基本韵味'.$template[0]['postFee'].'元<br>';
					$postfee = $postfee+$template[0]['postFee'];
				}
			}else{
				if(!$postSum){
					$parm['province']=$value['provinceId'];
					$parm['city']=$value['cityid'];
					$chinatemplate = rpcCall('chinatemplate.getByParm',$parm);

					if(count($chinatemplate)!= 0 ){

						array_push($expressPrice,$chinatemplate[0]['price']);
						$morethan = $chinatemplate[0]['morethan'];
						if($value['weight']>$morethan) {
							$str=$str.'&#60;';
							$str=$str.$parm[$key].'>重量为'.$value['weight'].'商品超过'.$morethan.'公斤,每公斤运费为'.$chinatemplate[0]['price'].'元<br>';
							$postfee = $postfee + ($value['weight'] - $morethan) / $chinatemplate[0]['price'];
						}
					}
					if($postSum ){

						$str=$str."&#60;".$parm[$key] .'>'.'加上普通基本韵味'.$template[0]['postFee'].'元<br>';
						$postfee = $postfee+$template[0]['postFee'];
					}
				}
			}
		}

		$parm['id'] = $value['specificationId'];
		$productexpresssku = rpcCall('productexpresssku.getById',$parm);
		$count= $count+$postfee+$productexpresssku[0]['price']*$value['number'];
	}
	if(!$postSum){
		$expressPrice = sort($expressPrice);
		$str=$str.'两件商品以上默认运费为'.$expressPrice[0].'元';
		$count= $count+$expressPrice[0];
	}
	$result['msg'] = $str;
	$result['money'] = $count;
	return $result;


}


function call($class,$parm){
		$class = str_replace(".","/",$class);
	$classInstall = substr($class,0,strpos($class,"/"));
	$method = substr($class,strripos($class,"/")+1);

	$reflect = new ReflectionClass($classInstall); // 建立 Person这个类的反射类  
	$instance  = $reflect->newInstanceArgs(); // 相当于实例化Person 类 
	$method = $reflect->getmethod($method); // 获取Person 类中的getName方法
	$result=$method->invokeArgs($instance, array($parm));
    return $result;
}

function getSession($key){

	return Session::instance()->get($key); //设置session值
}
function setsession($key,$value){

	Session::instance()->set($key,$value); //设置session值
}

function rpcCall($class,$parm){

	$class = str_replace(".","/",$class);
	$classInstall = substr($class,0,strpos($class,"/"));
	$method = substr($class,strripos($class,"/")+1);
   // echo 'application/classes/api/'.$classInstall.'.php';
    require_once ('application/classes/api/'.$classInstall.'.php');
	$reflect = new ReflectionClass($classInstall); // 建立 Person这个类的反射类  
	$instance  = $reflect->newInstanceArgs(); // 相当于实例化Person 类 
	$method = $reflect->getmethod($method); // 获取Person 类中的getName方法
	$result=$method->invokeArgs($instance, array($parm));
    return $result;
}
function getConfig(){
 	$result = self::rpcCall("pageconfig.getlist",null);
	
}
include ('application/classes/config/sysconfig.php');

function sendSMS($mobile,$content,$time='',$mid='')
{

	$config = new sysconfig();
	$content = urldecode($content);
	$http = 'http://api.wxduanxin.com/?'.'ac=send&uid='.$config->msgUser.'&pwd='.strtolower(md5($config->msgPassword)).'&mobile='.$mobile.'&content='.$content;

	$html = file_get_contents($http);
	echo $html;
	if($html==100){
		return true;
	}else{
		return false;
	}

}



function random($len)
{
	$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	mt_srand();
	$strs="";
	for($i=0;$i<$len;$i++){
		$strs.=$srcstr[mt_rand(0,35)];
	}
	return strtoupper($strs);
}
